package com.zzyl.nursing.controller.Member;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.dto.OverCountTimeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询取消预约信息次数
     */
    @GetMapping("/cancelled-count")
    @ApiOperation(value = "预取消预约次数")
    public R<Integer> selectReservationOver() {
        Integer count  = reservationService.selectReservationOver();
        if (count>3){
            throw new BaseException("预取消预约次数超过3次，请先取消已预约项目");
        }
        return R.ok(count);
    }

    /**
     * 查询时间段剩余的预约次数
     */
    @GetMapping("/countByTime")
    @ApiOperation(value = "时间段剩余的预约次数")
    public R<List<OverCountTimeDto>> selectReservationOverCount(Long time) {
        List<OverCountTimeDto> list = reservationService.selectReservationOverCount(time);
        return R.ok(list);
    }

    /**
     * 新增预约信息
     */
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation) {
        reservationService.insertReservationBy(reservation);
        return AjaxResult.success();
    }

    /**
     * 查询预约信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询预约信息")
    public R<TableDataInfo> list(Integer pageSize, Integer pageNum, Integer status) {
        TableDataInfo tableDataInfo = reservationService.selectReservationPage(pageSize, pageNum, status);
        return R.ok(tableDataInfo);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation(value = "取消预约")
    public AjaxResult cancel(@ApiParam(value = "预约ID", required = true) @PathVariable("id") Long id) {
        reservationService.updateReservationByID(id);
        return success();
    }

}
